package cn.myblog.ainblog.conroller;

import cn.myblog.ainblog.dto.CommentDTO;
import cn.myblog.ainblog.model.ArticleModel;
import cn.myblog.ainblog.model.CommentModel;
import cn.myblog.ainblog.service.*;
import cn.myblog.ainblog.vo.ArticleAndCategoryVO;
import cn.myblog.ainblog.vo.ArticleAndTagVO;
import cn.myblog.ainblog.vo.ArticleCategoryVO;
import cn.myblog.ainblog.vo.CommentReplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Dovaior
 * @Description 指定博文详情页面
 * @create 2021-04-21 17:21
 **/
@Controller
public class SingleController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;

    /**
     * 访问具体的某个博文
     * @param articleId 指定的博文id
     * @param model 用于封装传递的信息
     */
    @GetMapping("/single/{articleId}")
    public String singleByArticleId(@PathVariable String articleId, Model model){
        Map<String, Object> articleMap = articleService.queryArticleById(articleId);
        //分离博文表中的信息
        ArticleModel articleModel = (ArticleModel) articleMap.get("articleModel");
        model.addAttribute("articleModel",articleModel);
        //获取对应的分类信息
        ArticleAndCategoryVO articleAndCategoryVO = (ArticleAndCategoryVO) articleMap.get("articleAndCategoryVO");
        model.addAttribute("articleAndCategoryVO",articleAndCategoryVO);
        //获取对应的标签信息
        ArticleAndTagVO articleAndTagVO = (ArticleAndTagVO) articleMap.get("articleAndTagVO");
        model.addAttribute("articleAndTagVO",articleAndTagVO);
        //获取当前博文的评论
        List<CommentReplyVO> commentReplyVOS = commentService.queryAllCommentAndReplyMsgByArticleId(articleId);
        model.addAttribute("commentModels",commentReplyVOS);
        return "single";
    }

    /**
     * 查询nickName返回前端
     * 用于详情页面的作者展示
     */
    @ResponseBody
    @GetMapping("/queryNickName")
    public String queryNickName(){
        return userService.queryNickName();
    }

    /**
     * 相关推荐相同分类的排除指定博文的六条信息
     * @param categoryId 指定的分类id
     * @param articleId 指定排除的博文id
     */
    @ResponseBody
    @GetMapping("/queryArticleModelByCategoryLimit6/{categoryId}/{articleId}")
    public List<ArticleCategoryVO> queryArticleModelByCategoryLimit6(@PathVariable String categoryId,@PathVariable String articleId){
        Map<String ,String> map = new HashMap<>();
        map.put("categoryId",categoryId);
        map.put("articleId",articleId);
        return articleService.queryArticleModelByCategoryLimit6(map);
    }

    /**
     * 点赞事件====更新当前博文的点赞量，并且重新查询当前的博文的点赞数
     * @param articleId 指定的博文id
     */
    @ResponseBody
    @GetMapping("/likeCountPlusAndQuery/{articleId}")
    public Long updateLikeCount(@PathVariable String articleId){
        articleService.likeCountPlusAndQuery(articleId);
        Long likeCount = articleService.queryLikeCountById(articleId);
        return likeCount;
    }

    /**
     * 添加留言
     * @return 返回成功标识
     */
    @ResponseBody
    @PostMapping("/commentInsert")
    public String commentInsert(CommentDTO commentDTO){
        CommentModel commentModel = new CommentModel();
        //为 commentModel 赋值
        BeanUtils.copyProperties(commentDTO,commentModel);
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //为创建时间赋值
        commentModel.setCommentDate(df.format(new Date()));
        //添加评论信息
        commentService.commentInsert(commentModel);
        //更新该博文的评论数
        articleService.freshCommentCountByArticleId(commentModel.getArticleId());
        return "提交成功";
    }

    /**
     * 查询指定博文的评论
     * @param articleId 指定博文id
     * @param model 封装信息
     * @return 返回局部页面
     */
    @GetMapping("/queryAllCommentById/{articleId}")
    public String queryAllCommentById(@PathVariable String articleId,Model model){
        model.addAttribute("commentModels",commentService.queryAllCommentAndReplyMsgByArticleId(articleId));
        //返回局部页面实现局部刷新
        return "single::comment_refresh";
    }

    /**
     * 查询指定留言的回复状态
     * @param commentId 指定的留言id
     */
    @ResponseBody
    @GetMapping("/selectReplyStatus/{commentId}")
    public int selectReplyStatus(@PathVariable Long commentId){
        return commentService.isReplyStatus(commentId);
    }

    /**
     * 查询指定留言的回复内容
     * @param commentId 指定的留言id
     */
    @ResponseBody
    @GetMapping("/selectReplyByCommentId/{commentId}")
    public String selectReplyByCommentId(@PathVariable Long commentId){
        return replyService.queryReplyContentByCommentId(commentId);
    }
}
